package com.goldsprite.spinelearn.examples.gfsm;

import com.goldsprite.gdxcore.gfsm.Entity;
import com.goldsprite.spinelearn.examples.AnimType;
import com.goldsprite.spinelearn.examples.CharacterConfig;
import com.goldsprite.spinelearn.examples.SpineCharacter;
import com.goldsprite.spinelearn.examples.Env;

public class SpineEntity extends Entity {
	private SpineCharacter spineCharacter;
	private CharacterConfig config;

	private int facing = 1;
	private float positionX, positionY;

	public SpineEntity(SpineCharacter spineCharacter, CharacterConfig config) {
		this.spineCharacter = spineCharacter;
		this.config = config;
	}

	public void playAnim(AnimType type, boolean loop) {
		String animName = config.getAnimName(type);
		System.out.println(facing);
//		//左面真实转面
//		if (getFacing() == -1)
//			animName += "_L";
		//镜像转面
		spineCharacter.setFacingDirection(facing);
		if (animName != null) {
			spineCharacter.getAnimationState().setAnimation(0, animName, loop);
		}
	}

	public boolean isAnimationComplete() {
		return spineCharacter.getAnimationState().getCurrent(0).isComplete();
	}

	public boolean hasJumpAnimation() {
		return config.getJumpAnimation() != null;
	}

	public void updatePosition(float direction) {
		positionX += direction * Env.MOVE_SPEED * spineCharacter.getDeltaTime();
		positionX = Math.max(50, Math.min(Env.WORLD_WIDTH - 50, positionX));
		spineCharacter.setPosition(positionX, positionY);
	}

	public void updateFacing(float direction) {
		if (direction != 0) {
//			spineCharacter.setFacingDirection((int)Math.signum(direction));
			facing=(int)Math.signum(direction);
		}
	}

	public void setPosition(float x, float y) {
		this.positionX = x;
		this.positionY = y;
	}

	// Getter/Setter
	public float getPositionX() { return positionX; }
	public float getPositionY() { return positionY; }

	public void move(float dir) {
		if(dir == 0) return;

		updatePosition(dir);
		//updateFacing(dir);
	}

	public int getFacing() {
		return facing;
	}
}
